package com.monitor.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.monitor.entity.ErpCustomerCar;
import com.monitor.entity.ErpDriver;
import com.monitor.service.ErpCustomerCarService;
import com.monitor.service.ErpDriverService;
import com.monitor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆挂车对应表 前端控制器
 * </p>
 *
 * @author LX
 * @since 2018-09-13
 */
@RestController
@RequestMapping("/erpDriver")
@Transactional
@Api(description="车辆挂车对应")
public class ErpDriverController {

    @Resource
    private ErpDriverService driverService;

    @Resource
    private ErpCustomerCarService erpCustomerCarService;

    @RequestMapping("/inertOrUpdateDriver")
    @ApiOperation("添加Or修改车辆挂车对应信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")*/
    })
    public @ResponseBody
    Result<Integer> inertOrUpdateRepair(ErpDriver driverRepair) {
        Result<Integer>  result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(driverService.insertOrUpdate(driverRepair)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/getdriverBycustId")
    @ApiOperation("获取车辆挂车对应信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "custId", value = "客户ID", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<List<ErpDriver>> getdriverBycustId(Integer custId) {
        Result<List<ErpDriver>> result=new Result<List<ErpDriver>>();
        try{
            List<ErpDriver> customerCar=driverService.selectListByCar(custId);
            customerCar.parallelStream().forEach(car->{
                ErpCustomerCar erpCustomerCar = erpCustomerCarService.selectOne(new EntityWrapper<ErpCustomerCar>().eq("car_plate", car.getCarPlate()));
                if (erpCustomerCar!=null)
                car.setCarId(erpCustomerCar.getId());

            });
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }


    @RequestMapping("/getdriverByid")
    @ApiOperation("根据Pid获取车辆挂车对应信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "Id", value = "车辆挂车对应id", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<ErpDriver> getdriverRepairByPid(Integer Id) {
        Result<ErpDriver> result=new Result<ErpDriver>();
        try{
            ErpDriver customerCar=driverService.selectOne(new EntityWrapper<ErpDriver>().eq("id",Id));
            result.setCount(1);
            result.setData(customerCar);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }

    @RequestMapping("/removedriverById")
    @ApiOperation("根据id删除车辆挂车对应信息")
    @ApiImplicitParams({
           /* @ApiImplicitParam(name = "id", value = "id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "部门名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "parentId", value = "父id", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "customerCode", value = "客户编号", required = false, dataType = "int", paramType = "query"),*/
            @ApiImplicitParam(name = "Id", value = "Id", required = false, dataType = "int", paramType = "query")
    })
    public @ResponseBody
    Result<Integer> removedriverById(Integer Id) {
        Result<Integer> result=new Result<Integer>();
        try{
            result.setCount(1);
            result.setData(driverService.deleteById(Id)?1:0);
        }catch (Exception e){e.printStackTrace();}
        return result;
    }










}

